package com.jm3.riscar3.alu;

import com.jm3.riscar3.helpers.BinaryTranslator;

public class ALUUtils {

	public static final int EIGHT_BIT = 128;
	public static final int NINTH_BIT = 256;
	public static final int FOURTH_BIT = 8;
	public static final int EIGHT_BIT_MASK = 0xFF;
	public static final int NIBBLE_MASK = 0x0F;

	public static String fromIntToBinaryString(int result, int digits) {
		return String.format("%0" + digits + "d",
				Integer.parseInt(Integer.toBinaryString(result)));

	}

	public static int twosComplement(int signed) {
		if (signed < 0) {
			signed = -1 * signed;
			return (~signed & 255) + 1;
		}

		return signed;
	}

	public static int getTwosComplement(int unsigned) {

		return (~unsigned & 255) + 1;

	}

	public static int getOnesComplement(int unsigned) {

		return (~unsigned & 255);

	}

	public static boolean isEightBitNegative(int result) {
		return (result & EIGHT_BIT) != 0;
	}

	public static boolean isFourBitNegative(int result) {
		return (result & FOURTH_BIT) != 0;
	}

	public static boolean isZero(int result) {
		return result == 0;
	}

	public static boolean isBinaryNegative(String result) {
		return isEightBitNegative(Integer.parseInt(result, 2));
	}

	public static boolean isSumOverflow(int operand1, int operand2, int result) {
		boolean isPositive1 = !isEightBitNegative(operand1);
		boolean isPositive2 = !isEightBitNegative(operand2);
		boolean positiveResult = !isEightBitNegative(result);

		if ((isPositive1 && isPositive2) && !positiveResult)
			return true;

		if ((!isPositive1 && !isPositive2) && positiveResult)
			return true;

		return false;
	}

	public static boolean isSumCarry(int operand1, int operand2, int result) {

		if (operand1 > operand2) {
			if (result < operand1)
				return true;
		} else {
			if (result < operand2)
				return true;
		}
		return false;
	}

	public static boolean isMultOverflow(int operand1, int operand2, int result) {
		boolean isPositive1 = !isFourBitNegative(operand1);
		boolean isPositive2 = !isFourBitNegative(operand2);
		boolean positiveResult = !isEightBitNegative(result);

		if ((isPositive1 && isPositive2) && !positiveResult)
			return true;

		if ((!isPositive1 && !isPositive2) && !positiveResult)
			return true;

		if ((isPositive1 && !isPositive2) && positiveResult)
			return true;

		if ((!isPositive1 && isPositive2) && positiveResult)
			return true;

		return false;
	}

	public static String fromIntToHexString(int result) {
		return String.format("%02d",
				Integer.parseInt(Integer.toHexString(255 & result)));
	}

	public static int getLowEightBitArgument(String args) {

		int bitValue = 0;

		try {
			bitValue = BinaryTranslator.getBitValue(args.substring(3));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bitValue;
	}

	public static int getRegisterNumber(String args) {
		String regFromArgs = args.substring(0, 3);
		int regNumber = 0;
		try {
			regNumber = BinaryTranslator.getBitValue((regFromArgs));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return regNumber;
	}
}
